﻿#include "serverinfopushcenter.h"
#include "servermonitorcenter.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <qglobalconfig.h>
#include <tcpsocketnetworkcenter.h>
#include <udpsocketnetworkcenter.h>
#include <websocketnetworkcenter.h>

QMutex ServerInfoPushCenter::mutex;
ServerInfoPushCenter *ServerInfoPushCenter::instance = nullptr;

ServerInfoPushCenter *ServerInfoPushCenter::getInstance()
{
    if (instance == nullptr)
    {
        QMutexLocker locker(&mutex);
        if (instance == nullptr)
        {
            instance = new ServerInfoPushCenter();
        }
    }
    return instance;
}

void ServerInfoPushCenter::deleteInstance()
{
    if (instance != nullptr)
    {
        delete instance;
        instance = nullptr;
    }
}

ServerInfoPushCenter::ServerInfoPushCenter(QObject *parent)
    : QThread(parent)
{
    qDebug() << __FUNCTION__;
    moveToThread(this);
    pushInterval = QGlobalConfig::getInstance().read("pushWork", "pushInterval", 2000).toUInt();
    start();
}

ServerInfoPushCenter::~ServerInfoPushCenter()
{
    qDebug() << __FUNCTION__;
    isRun = false;
    quit();
    wait();
}

void ServerInfoPushCenter::run()
{
    bool tcpSocketOpen = QGlobalConfig::getInstance().read("netWork", "tcpSocketOpen", true).toBool();
    bool udpSocketOpen = QGlobalConfig::getInstance().read("netWorke", "udpSocketOpen", true).toBool();
    bool webSocketOpen = QGlobalConfig::getInstance().read("netWork", "webSocketOpen", true).toBool();

    isRun = true;
    while (isRun)
    {
        QJsonObject jsonObject;

        ServerInfo serverInfo;
        ServerMonitorCenter::getInstance()->getServerInfo(serverInfo);
        jsonObject.insert("serverInfo", serverInfo.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));

        QJsonArray cpuInfoJsonArray;
        QList<CpuInfo> cpuInfoList;
        ServerMonitorCenter::getInstance()->getCpuInfoList(cpuInfoList);
        for (CpuInfo &info : cpuInfoList)
        {
            cpuInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("cpuInfoList", cpuInfoJsonArray);

        QJsonArray memoryInfoJsonArray;
        QList<MemoryInfo> memoryInfoList;
        ServerMonitorCenter::getInstance()->getMemoryInfoList(memoryInfoList);
        for (MemoryInfo &info : memoryInfoList)
        {
            memoryInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("memoryInfoList", memoryInfoJsonArray);

        QJsonArray diskInfoJsonArray;
        QList<DiskInfo> diskInfoList;
        ServerMonitorCenter::getInstance()->getDiskInfoList(diskInfoList);
        for (DiskInfo &info : diskInfoList)
        {
            diskInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("diskInfoList", diskInfoJsonArray);

        QJsonArray cpuUseJsonArray;
        QList<CpuUse> cpuUseList;
        ServerMonitorCenter::getInstance()->getCpuUseList(cpuUseList);
        for (CpuUse &use : cpuUseList)
        {
            cpuUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime", "selfUsageRate"}));
        }
        jsonObject.insert("cpuUseList", cpuUseJsonArray);

        QJsonArray memoryUseJsonArray;
        QList<MemoryUse> memoryUseList;
        ServerMonitorCenter::getInstance()->getMemoryUseList(memoryUseList);
        for (MemoryUse &use : memoryUseList)
        {
            memoryUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("memoryUseList", memoryUseJsonArray);

        QJsonArray virtualMemoryUseJsonArray;
        QList<MemoryUse> virtualMemoryUseList;
        ServerMonitorCenter::getInstance()->getVirtualMemoryUseList(virtualMemoryUseList);
        for (MemoryUse &use : virtualMemoryUseList)
        {
            virtualMemoryUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("virtualMemoryUseList", virtualMemoryUseJsonArray);

        QJsonArray diskUseJsonArray;
        QList<DiskUse> diskUseList;
        ServerMonitorCenter::getInstance()->getDiskUseList(diskUseList);
        for (DiskUse &use : diskUseList)
        {
            diskUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("diskUseList", diskUseJsonArray);

        DiskUse diskUseAll = ServerMonitorCenter::getInstance()->getDiskUseAll();
        jsonObject.insert("diskUseAll", diskUseAll.toJsonObject(QBaseEntity::UnEmptyProperty));

        QJsonArray networkInfoJsonArray;
        QList<NetworkInfo> networkInfoList;
        ServerMonitorCenter::getInstance()->getNetworkInfoList(networkInfoList);
        for (NetworkInfo &item : networkInfoList)
        {
            networkInfoJsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("networkInfoList", networkInfoJsonArray);

        QJsonArray networkUseJsonArray;
        QList<NetworkUse> networkUseList;
        ServerMonitorCenter::getInstance()->getNetworkUseList(networkUseList);
        for (NetworkUse &item : networkUseList)
        {
            networkUseJsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
        }
        jsonObject.insert("networkUseList", networkUseJsonArray);

        QString message = QJsonDocument(jsonObject).toJson(QJsonDocument::Compact);

        if (tcpSocketOpen)
        {
            TcpSocketNetWorkCenter::getInstance()->sendMessage(message);
        }
        if (udpSocketOpen)
        {
            UdpSocketNetWorkCenter::getInstance()->sendMessage(message);
        }
        if (webSocketOpen)
        {
            WebSocketNetWorkCenter::getInstance()->sendMessage(message);
        }

        int sleepCount = pushInterval / 10;
        while (isRun && sleepCount > 0)
        {
            QThread::msleep(10);
            sleepCount--;
        }
    }
}
